<?php
require_once 'MyPdoCounty.class.php';
class TableShopGoods extends MyPdoCounty{
	private $tableName = 'shop_goods';	//表名



	//商品的删除
	function _storeGoodsDel($goods_id,$store_id){
		//删除商品数据
		$sql = "delete ignore from {$this->tableName} where store_id=? and id = ?";
		return $this->dml($sql,array($store_id,$goods_id));
	}



	//商品的上下架
	function _goodsUpOrDown($id,$stat){
		$stat = $stat==2?5:2;
		$sql = "update {$this->tableName} set goods_stat=? where id=? and store_stat=1 and goods_stat!=4";
		return $this->dml($sql,array($stat,$id));
	}


	//修改商品状态
	function _changeGoodsStat($id,$stat){
		$sql = "update {$this->tableName} set goods_stat=? where id=?";
		return $this->dml($sql,array($stat,$id));
	}


	//将商品状态重置为待审
	function _resetGoodsStat($goods_id){
		$sql = "update {$this->tableName} set goods_stat = 1 where id = ? and store_stat = 1 and goods_stat !=4";
		return $this->dml($sql,array($goods_id));
	}


	//通过id获取指定商品信息
	function _getGoodsInfoForId($goods_id,$fields = false){
		if(!$fields){
			$fields = 'id,tid1,tid2,tid3,type_set,title,price,info,number,banner';
		}
		$sql = "select {$fields} from {$this->tableName} where id = ?";
		return $this->dql($sql,array($goods_id),1);
	}



	//商品的修改
	public function _storeGoodsEdit($goods_id,$t1,$t2,$t3,$type_set,$banner,$title,$number,$price,$info){
		$sql = "update {$this->tableName} set tid1=?,tid2=?,tid3=?,type_set=?,title=?,number=?,price=?,info=?,banner=?,goods_stat=1 where id=? and store_stat = 1 and goods_stat !=4";
		return $this->dml($sql,array($t1,$t2,$t3,$type_set,$title,$number,$price,$info,$banner,$goods_id));
	}




	/*
		### 添加商品
		$store_id	商户id
		$t1  		一级商品类别id
		$t2  		二级商品类别id
		$t3  		三级商品类别id
		$typeSet	商户类别名称集合
		$title 		商品名
		$number 	商品编号
		$price 		商品价格
		$info 		商品信息
	*/
	public function _storeGoodsAdd($store_id,$t1,$t2,$t3,$typeSet,$title,$number,$price,$info){
		$sqlc = "select count(*) as c from {$this->tableName} where store_id = ?";
		$resc = $this->dql($sqlc,array($store_id),1);
		if(@$resc && @$resc['c'] > $this->config['goodsMaxCount']){
			return 'err2';	//超过商品条数限制
		}

		$sql = "insert into {$this->tableName}(store_id,tid1,tid2,tid3,type_set,title,price,info,number,ctime) values(?,?,?,?,?,?,?,?,?,?)";
		$arr = array(
			$store_id,
			$t1,
			$t2,
			$t3,
			$typeSet,
			$title,
			$price,
			$info,
			$number,
			time()
		);
		$res = $this->dml($sql,$arr);
		if($res){
			return 'ok';
		}else{
			return 'err3';	//添加数据异常
		}
	}



	//通过商户id获取商户下的商品列表（关联查询砍价状态）
	public function _getGoodsListForStoreId($store_id,$fields=false){
		$sql = "select g.id,banner,g.title,g.price,g.goods_stat,g.number,g.type_set,f.id as free_id
from shop_goods as g left join shop_free as f on g.id = f.goods_id where g.store_id = ?";
		return $this->dql($sql,array($store_id));
	}



	//获取所有商品列表
	public function _getGoodsListFull($fields = false){
		$fields = $fields?:'*';
		$pnow = intval(I('pnow','post'));
		$pnow = $pnow > 0?$pnow:1;
		$type_sel = I('type_sel','post');
		$search = I('search','post');				//可有
		$store_id = intval(I('store_id','post'));	//可有
		$limit = $this->config['psize'];
		$start = ($pnow-1)*$limit;
		switch($type_sel){
			case '1':case 1:$where = 'where goods_stat = 1';break;
			case '2':case 2:$where = 'where goods_stat = 2';break;
			case '3':case 3:$where = 'where goods_stat = 3';break;
			case '4':case 4:$where = 'where goods_stat = 4';break;
			case '5':case 5:$where = 'where goods_stat = 5';break;
			default:$where = '';
		}
		$arr = array();
		if($where && $store_id){
		}
		if($store_id){
			if($where){
				$where .= ' and store_id = ?';
			}else{
				$where = 'where store_id = ?';
			}
			array_push($arr,$store_id);
		}
		if($search){
			if($where){
				$where .= ' and title like ?';
			}else{
				$where = 'where title like ?';
			}
			array_push($arr,"%$search%");
		}
		$sql = "select {$fields} from {$this->tableName} {$where} limit {$start},{$limit}";
		return $this->dql($sql,$arr);
	}
}